﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Drawing;

namespace 车辆违章查询
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void WebReq(string req)
        {
            HttpWebRequest request = null;
            request = WebRequest.Create("http://www.hljjj.gov.cn:8081/Home/getWfcx") as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Headers["x-requested-with"] = "XMLHttpRequest";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Accept = "application/json, text/javascript, */*";
            request.Headers["Accept-Encoding"] = "gzip, deflate";
            request.Host = "www.hljjj.gov.cn:8081";
            request.ContentLength = 81;
            request.KeepAlive = true;
            request.Headers["Cache-Control"] = "no-cache";

            string str = req;//;
            byte[] data = Encoding.UTF8.GetBytes(str);
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            HttpWebResponse re = request.GetResponse() as HttpWebResponse;
            System.IO.StreamReader Reader = new System.IO.StreamReader(re.GetResponseStream(), System.Text.Encoding.GetEncoding(re.CharacterSet));
            str = Reader.ReadToEnd();
            str = str.Replace("\\u003c", "<");
            str = str.Replace("\\u003e", ">");
            str = str.Replace("\\u0027", "'");
            string str1, str2;
            listView1.Items.Clear();
            int index = 0, index1;
            while (true)
            {
                index = str.IndexOf("黑ASD626", index);
                if (index == -1)
                    break;
                index = index + "黑ASD626</td><td>小型汽车</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//违法时间
                ListViewItem li = listView1.Items.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//金额
                li.SubItems.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//计分
                li.SubItems.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//缴款状态
                li.SubItems.Add(str2);
                if (str2 == "已缴款")
                    li.BackColor = SystemColors.Highlight;
                else
                    li.BackColor = Color.IndianRed;
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//处理状态
                li.SubItems.Add(str2);
                if (str2 == "已处理")
                    li.BackColor = SystemColors.Highlight;
                else
                    li.BackColor = Color.IndianRed;
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//违法地点
                li.SubItems.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//违法内容
                li.SubItems.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//决定书编号
                li.SubItems.Add(str2);
                index = index1 + "</td><td>".Length;
                str1 = str.Substring(index);
                index1 = str.IndexOf("</td>", index);
                str2 = str.Substring(index, index1 - index);//采集机关
                li.SubItems.Add(str2);
            }
            if (listView1.Items.Count == 0)
                MessageBox.Show("恭喜!!!!当前查询无结果");
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            WebReq("hpzl=02&dy=黑&xzqh=A&hphm=SD626&clsbdh=LDC611P28A1329637&jkbj=%&ts=1364896563002");
//             IDictionary<string,string> parameters=new Dictionary<string,string>();
//             parameters.Add("hpzl","02");
//             parameters.Add("dy","黑");
//             parameters.Add("xzqh","A");
//             parameters.Add("hphm","SD626");
//             parameters.Add("clsbdh","LDC611P28A1329637");
//             parameters.Add("jkbj","%");
//             parameters.Add("ts","1364896563002");

            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            WebReq("hpzl=02&dy=黑&xzqh=A&hphm=SD626&clsbdh=LDC611P28A1329637&jkbj=0&ts=1364896563002");
        }
    }
}
